//
//  ViewController.m
//  OffScreenDemo
//
//  Created by 梁立保 on 2019/6/13.
//  Copyright © 2019 梁立保. All rights reserved.
//

#import "ViewController.h"
#import "TestTableViewCell.h"
#import "UIView+ATFrame.h"
#import "UIImage+Clicp.h"
#import "TestModel.h"

#define KMainW [UIScreen mainScreen].bounds.size.width


@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic) NSMutableArray <TestModel *>*dataArr;
@end

@implementation ViewController

- (NSMutableArray<TestModel *> *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
        for (int i = 0; i<100; i++) {
            TestModel *model =  [[TestModel alloc] init];
            model.height = 120;
            [_dataArr addObject:model];
        }
    }
    return _dataArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self creatControl];
//    return;
    // Do any additional setup after loading the view.
    [self.view addSubview:({
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, self.view.bounds.size.height - 100)];
        tableView.dataSource = self;
        tableView.delegate = self;
        [tableView registerClass:TestTableViewCell.class forCellReuseIdentifier:NSStringFromClass(self.class)];
        tableView;
    })];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(self.class) forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 100;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    TestModel *model = [self.dataArr objectAtIndex:indexPath.row];
    return model.height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    TestModel *model = [self.dataArr objectAtIndex:indexPath.row];
    //TestTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (model.height == 10) {
        model.height = 120;
    } else {
        model.height = 10;
    }
    [UIView performWithoutAnimation:^{
        [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    }];
}

- (void)creatControl {
    CGFloat margin = 20.f;
    CGFloat controlW = (KMainW - margin * 3) * 0.5;
    
    NSArray *titleArray = @[@"离屏渲染", @"非离屏渲染"];
    for (int i = 0; i < titleArray.count; i++) {
        CGFloat controlX = margin + (controlW + margin) * i;
        //UILabel
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(controlX, 30, controlW, 40)];
        label.layer.backgroundColor = [[UIColor grayColor] CGColor];
        label.text = titleArray[i];
        label.textAlignment = NSTextAlignmentCenter;
        label.layer.cornerRadius = label.bounds.size.height * 0.5;
        label.layer.masksToBounds = i == 0 ? YES : NO;
        [self.view addSubview:label];
        
        //UIView
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(controlX, CGRectGetMaxY(label.frame) + margin, controlW, 40)];
        view.backgroundColor = [UIColor grayColor];
        view.layer.cornerRadius = view.bounds.size.height * 0.5;
        view.layer.masksToBounds = i == 0 ? YES : NO;
        [self.view addSubview:view];
        
        //UIView若未添加子控件，设置view.layer.masksToBounds = YES;也不会造成离屏渲染
        UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(30, 0, controlW - 60, 40)];
        subView.backgroundColor = [UIColor redColor];
        [view addSubview:subView];
        
        //UITextView
        UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(controlX, CGRectGetMaxY(view.frame) + margin, controlW, 40)];
        textView.userInteractionEnabled = NO;
        textView.backgroundColor = [UIColor grayColor];
        if (i == 0) {
            /*
             这里换了一种实现方法，用UIBezierPath赋值layer.mask，两种方式都会造成离屏渲染
             textView.layer.cornerRadius = textView.bounds.size.height * 0.5;
             textView.layer.masksToBounds = YES;
             */
            UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:textView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(textView.bounds.size.height * 0.5, textView.bounds.size.height * 0.5)];
            CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
            maskLayer.frame = textView.bounds;
            maskLayer.path = path.CGPath;
            textView.layer.mask = maskLayer;
        }else {
            textView.layer.cornerRadius = textView.bounds.size.height * 0.5;
            textView.layer.masksToBounds = NO;
        }
        [self.view addSubview:textView];
        
        //UIButton
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(controlX, CGRectGetMaxY(textView.frame) + margin, controlW, controlW)];
        if (i == 0) {
            [button setImage:[UIImage imageNamed:@"hero_1"] forState:UIControlStateNormal];
            button.layer.cornerRadius = button.bounds.size.width * 0.5;
            button.layer.masksToBounds = YES;
        }else {
            [button setImage:[[UIImage imageNamed:@"hero_1"] drawCornerInRect:button.bounds cornerRadius:button.bounds.size.width * 0.5] forState:UIControlStateNormal];
        }
        [self.view addSubview:button];
        
        //UIImageView设置圆角
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(controlX, CGRectGetMaxY(button.frame) + margin, controlW, controlW)];
        if (i == 0) {
            [imageView setImage:[UIImage imageNamed:@"hero_1"]];
            imageView.layer.cornerRadius = imageView.bounds.size.width * 0.5;
            imageView.layer.masksToBounds = YES;
        }else {
            [imageView setImage:[[UIImage imageNamed:@"hero_1"] drawCornerInRect:imageView.bounds cornerRadius:imageView.bounds.size.width * 0.5]];
        }
        [self.view addSubview:imageView];
        
        //UIImageView若未添加子控件，设置imageView.layer.masksToBounds = YES;也不会造成离屏渲染
        UIView *subImageView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, controlW, 40)];
        subImageView.backgroundColor = [UIColor redColor];
        subImageView.layer.cornerRadius = imageView.bounds.size.width * 0.5;
        [imageView addSubview:subImageView];
        
        //UIImageView设置阴影
        CGFloat imgW = 70.f;
        CGFloat imgPadding = (KMainW - imgW * 4 - margin * 2) / 3;
        UIImageView *shadowImgView = [[UIImageView alloc] initWithFrame:CGRectMake(margin + (imgW + imgPadding) * 2 * i, CGRectGetMaxY(imageView.frame) + margin, imgW, imgW)];
        [shadowImgView setImage:[UIImage imageNamed:@"hero_1"]];
        shadowImgView.layer.shadowColor = [UIColor redColor].CGColor;
        shadowImgView.layer.shadowOpacity = 0.8f;
        shadowImgView.layer.shadowOffset = CGSizeMake(5, 5);
        shadowImgView.layer.shadowRadius = 5.f;
        if (i == 1) {
            UIBezierPath *path = [UIBezierPath bezierPathWithRect:shadowImgView.bounds];
            shadowImgView.layer.shadowPath = path.CGPath;
        }
        [self.view addSubview:shadowImgView];
        
        //UIImageView设置阴影+圆角
        UIImageView *shadowCorImgView = [[UIImageView alloc] initWithFrame:CGRectMake(margin + imgW + imgPadding + (imgW + imgPadding) * 2 * i, CGRectGetMinY(shadowImgView.frame), imgW, imgW)];
        [shadowCorImgView setImage:[[UIImage imageNamed:@"hero_1"] drawCornerInRect:imageView.bounds cornerRadius:imageView.bounds.size.width * 0.5]];
        shadowCorImgView.layer.shadowColor = [UIColor redColor].CGColor;
        shadowCorImgView.layer.shadowOpacity = 0.8f;
        shadowCorImgView.layer.shadowOffset = CGSizeMake(0, 0);
        shadowCorImgView.layer.shadowRadius = 5.f;
        if (i == 1) {
            UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:shadowCorImgView.bounds cornerRadius:shadowCorImgView.bounds.size.height * 0.5];
            shadowCorImgView.layer.shadowPath = path.CGPath;
        }
        [self.view addSubview:shadowCorImgView];
    }
}
@end
